package com.caimiao.miao.widget;

import android.app.Dialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.caimiao.miao.R;

public class BasePromptDialog implements View.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnCancelListener {
    private static final float RATIO = 4f / 5f;
    private Dialog mDialog;
    private TextView mTvTitle;
    private FrameLayout mFlContent;
    private Button mBtnLeft, mBtnMid, mBtnRight;
    private ImageView mIvLineHor, mIvLineVer, mIvLineVer2;
    private OnBtnClickListener mOnBtnClickListener;
    private OnDialogHideListener mOnDialogHideListener;
    private DialogInterface.OnDismissListener mOnDismissListener;
    private DialogInterface.OnCancelListener mOnCancelListener;

    public interface OnBtnClickListener {
        void onLeft(BasePromptDialog dialog, View view);

        void onMiddle(BasePromptDialog dialog, View view);

        void onRight(BasePromptDialog dialog, View view);
    }

    public interface OnDialogHideListener {
        void hide();
    }

    public static class OnSimpleBtnClickListener implements OnBtnClickListener {

        @Override
        public void onLeft(BasePromptDialog dialog, View view) {
        }

        @Override
        public void onMiddle(BasePromptDialog dialog, View view) {
        }

        @Override
        public void onRight(BasePromptDialog dialog, View view) {
        }
    }

    @Override
    public void onClick(View view) {
        if (mOnBtnClickListener != null) {
            if (view.getId() == R.id.prompt_dialog_btn_left) {
                mOnBtnClickListener.onLeft(this, view);
            } else if (view.getId() == R.id.prompt_dialog_btn_right) {
                mOnBtnClickListener.onRight(this, view);
            } else if (view.getId() == R.id.prompt_dialog_btn_mid) {
                mOnBtnClickListener.onMiddle(this, view);
            }
        }
        mDialog.dismiss();
    }

    public BasePromptDialog(Context context) {
        mDialog = new Dialog(context, R.style.common_dialog_radius);
        View root = LayoutInflater.from(context).inflate(R.layout.prompt_dialog, null);
        mTvTitle = root.findViewById(R.id.prompt_dialog_tv_title);
        mFlContent = root.findViewById(R.id.prompt_dialog_content);
        mBtnLeft = root.findViewById(R.id.prompt_dialog_btn_left);
        mBtnRight = root.findViewById(R.id.prompt_dialog_btn_right);
        mBtnMid = root.findViewById(R.id.prompt_dialog_btn_mid);
        mIvLineHor = root.findViewById(R.id.prompt_dialog_iv_line_hor);
        mIvLineVer = root.findViewById(R.id.prompt_dialog_iv_line_ver);
        mIvLineVer2 = root.findViewById(R.id.prompt_dialog_iv_line_ver2);
        mBtnLeft.setOnClickListener(this);
        mBtnRight.setOnClickListener(this);
        mDialog.setContentView(root, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if (mOnDialogHideListener != null) mOnDialogHideListener.hide();
            }
        });
    }

    protected BasePromptDialog setContent(View view) {
        mFlContent.addView(view);
        return this;
    }

    public BasePromptDialog setDisableCancel() {
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
        return this;
    }

    public BasePromptDialog setBtnCount(int count) {
        if (count == 1) {
            mBtnMid.setVisibility(View.GONE);
            mBtnRight.setVisibility(View.GONE);
            mIvLineVer.setVisibility(View.GONE);
            mIvLineVer2.setVisibility(View.GONE);
        } else if (count == 2) {
            mBtnMid.setVisibility(View.GONE);
            mIvLineVer2.setVisibility(View.GONE);
        } else if (count == 3) {
            mBtnMid.setVisibility(View.VISIBLE);
            mBtnRight.setVisibility(View.VISIBLE);
            mIvLineVer.setVisibility(View.VISIBLE);
            mIvLineVer2.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public BasePromptDialog setHideTitle() {
        mTvTitle.setVisibility(View.GONE);
        mIvLineHor.setVisibility(View.GONE);
        return this;
    }

    public BasePromptDialog setTitle(int title) {
        mTvTitle.setText(title);
        return this;
    }

    public BasePromptDialog setLeft(int left) {
        mBtnLeft.setText(left);
        return this;
    }

    public BasePromptDialog setMid(int mid) {
        mBtnMid.setText(mid);
        return this;
    }

    public BasePromptDialog setRight(int right) {
        mBtnRight.setText(right);
        return this;
    }

    public BasePromptDialog setBtnColor(Integer left, Integer mid, Integer right) {
        if (left != null) mBtnLeft.setTextColor(left);
        if (mid != null) mBtnMid.setTextColor(mid);
        if (right != null) mBtnRight.setTextColor(right);
        return this;
    }

    public BasePromptDialog setOnHideLister(final OnDialogHideListener lister) {
        mOnDialogHideListener = lister;
        return this;
    }

    public BasePromptDialog setOnBtnClickListener(OnBtnClickListener listener) {
        mOnBtnClickListener = listener;
        return this;
    }

    public BasePromptDialog setDialogWidth(Context context) {
        return setDialogWidth(context, RATIO);
    }

    public BasePromptDialog setDialogWidth(Context context, float ratio) {
        int screenWidth = getScreenWidth(context);
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = Math.round(screenWidth * ratio);
        mDialog.getWindow().setAttributes(params);
        return this;
    }

    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        mDialog.dismiss();
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (mOnDialogHideListener != null) mOnDialogHideListener.hide();
        if (mOnCancelListener != null) mOnCancelListener.onCancel(dialog);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (mOnDialogHideListener != null) mOnDialogHideListener.hide();
        if (mOnDismissListener != null) mOnDismissListener.onDismiss(dialog);
    }


    private int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        int[] screens = new int[2];
        screens[0] = metrics.widthPixels;
        screens[1] = metrics.heightPixels;
        return screens[0];
    }

}
